package com.list88zhu.common.rxhttp.http;

import com.alibaba.fastjson.JSONObject;
import com.list88zhu.common.rxhttp.http.entity.BaseEntity;
import com.list88zhu.common.rxhttp.http.exception.HttpResultException;
import com.trello.rxlifecycle.components.support.RxAppCompatActivity;
import java.lang.ref.SoftReference;
import retrofit2.Retrofit;
import rx.Observable;
import rx.functions.Func1;

/**
 * projectName rxretrofit
 * date 2017/6/21
 * description com.example.administrator.rxjavaretrofitsample.http
 */

public abstract class BaseApi implements Func1<String, String> {

    /*rx生命周期管理*/
    private SoftReference<RxAppCompatActivity> rxAppCompatActivity;
    /*回调*/
    private HttpOnNextListener listener;
    /*是否是第三方接口，直接返回相应*/
    private boolean isThirdInterface = false;

    /*是否能取消加载*/
    private boolean cancel;
    /*是否显示加载框*/
    private boolean showProgress;
    /*是否需要缓存*/
    private boolean cache;
    /*url*/
    private String baseUrl = "https://free-api.heweather.com/v5/";
    /*方法-如果需要缓存必须设置这个参数；不需要不用設置*/
    private String method = "";
    /*超时时间-默认6秒*/
    private int connectionTime = 6;
    /*有网情况下的本地缓存时间默认60秒*/
    private int cookieNetworkTime = 60;
    /*无网络的情况下本地缓存时间默认30天*/
    private int cookieNoNetworkTime = 60*60*24*30;

    public BaseApi(HttpOnNextListener listener, RxAppCompatActivity activity){
        this.listener = listener;
        rxAppCompatActivity = new SoftReference<>(activity);
        isThirdInterface = false;
    }

    public abstract Observable getObservable(Retrofit retrofit);

    @Override
    public String call(String baseEntity) {
        //系统接口处理后返回
        if(!isThirdInterface){
            BaseEntity baseEntity1 = JSONObject.parseObject(baseEntity, BaseEntity.class);
            if(!baseEntity1.getMeta().isSuccess()){
                if(baseEntity1.getMeta().getCode() == 610) { //重新登录
                    relogin();
                }
                throw new HttpResultException(baseEntity1.getMeta().getMessage());
            }
            return baseEntity1.getData();
        }else{ //第三方接口直接返回
            return baseEntity;
        }
    }

    public HttpOnNextListener getListener() {
        return listener;
    }

    public void setListener(HttpOnNextListener listener) {
        this.listener = listener;
    }

    public boolean isCancel() {
        return cancel;
    }

    public void setCancel(boolean cancel) {
        this.cancel = cancel;
    }

    public boolean isShowProgress() {
        return showProgress;
    }

    public void setShowProgress(boolean showProgress) {
        this.showProgress = showProgress;
    }

    public boolean isCache() {
        return cache;
    }

    public void setCache(boolean cache) {
        this.cache = cache;
    }

    public String getBaseUrl() {
        return baseUrl;
    }

    public void setBaseUrl(String baseUrl) {
        this.baseUrl = baseUrl;
    }

    public String getMethod() {
        return method;
    }

    public void setMethod(String method) {
        this.method = method;
    }

    public String getUrl() {
        return baseUrl + method;
    }

    public int getConnectionTime() {
        return connectionTime;
    }

    public void setConnectionTime(int connectionTime) {
        this.connectionTime = connectionTime;
    }

    public int getCookieNetworkTime() {
        return cookieNetworkTime;
    }

    public void setCookieNetworkTime(int cookieNetworkTime) {
        this.cookieNetworkTime = cookieNetworkTime;
    }

    public int getCookieNoNetworkTime() {
        return cookieNoNetworkTime;
    }

    public void setCookieNoNetworkTime(int cookieNoNetworkTime) {
        this.cookieNoNetworkTime = cookieNoNetworkTime;
    }

    public RxAppCompatActivity getRxAppCompatActivity() {
        return rxAppCompatActivity.get();
    }

    public void setRxAppCompatActivity(RxAppCompatActivity rxAppCompatActivity) {
        this.rxAppCompatActivity = new SoftReference<RxAppCompatActivity>(rxAppCompatActivity);
    }

    /*重新登录*/
    private void relogin(){
//        BaseApplication.spUserInfo.clear();
//        BaseApplication.getInstance().getActivityManager().popAllActivity();
//        Intent intent = new Intent(BaseApplication.getInstance().getApplicationContext(), StartActivity.class);
//        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
//        BaseApplication.getInstance().getApplicationContext().startActivity(intent);
    }

    public boolean isThirdInterface() {
        return isThirdInterface;
    }

    public void setThirdInterface(boolean thirdInterface) {
        isThirdInterface = thirdInterface;
    }
}
